<?php

namespace app\model\v1\repair;

use crmeb\traits\ModelTrait;
use think\facade\Db;
use think\Model;

class RepairCat extends Model
{

	use ModelTrait;

	protected $updateTime = false;
	protected $createTime = false;

	protected $hidden = [
		//    'create_time','update_time','delete_time'
	];





	/**
	 * 维修项目分类管理列表
	 * @param $param
	 * @return \think\Paginator
	 * @throws \think\db\exception\DbException
	 * @throws \think\exception\DbException
	 */
	public static function getRepairCatList($param = [])
	{
		$data["data"] = self::field ("*, FROM_UNIXTIME(create_time) as create_time,
            case when status=1 then '启用' 
            when status=2 then '禁用' 
            end as status_text 
            ")
			->where(function ($query) use ($param){
				$query->whereNull("delete_time");
				if(!empty($param['time'])){    //根据创建日期搜索
					$wheretime = explode (',',$param['time']);
					$query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
				}
				if(!empty($param["word"])) $query->where("name",'like', '%'.$param["word"].'%');
				if(!empty($param["pid"])) $query->where("pid", $param["pid"]);
			})
			->order("id", "desc")
			->page($param["page"], $param['limit'])
			->select()->toArray();

		$data["count"] = self::field ("*,
            case when status=1 then '启用' 
            when status=2 then '禁用' 
            end as status_text 
            ")
			->where(function ($query) use ($param){
				$query->whereNull("delete_time");
				if(!empty($param['time'])){    //根据创建日期搜索
					$wheretime = explode (',',$param['time']);
					$query->where('create_time', 'between', [strtotime($wheretime[0]),strtotime($wheretime[1])]);
				}
				if(!empty($param["word"])) $query->where("name",'like', '%'.$param["word"].'%');
				if(!empty($param["pid"])) $query->where("pid", $param["pid"]);
			})
			->count();

		if(!empty($data["data"])){
			foreach ($data["data"] as $key => &$val){
				$val["pid_name"] = self::getPidName($val["pid"]);
			}
		}

		return $data;
	}


	public static function getPidName($pid)
	{
		$arr = [];
		$parent = self::where("id", $pid)->find();
		if(!empty($parent)){
			$arr[] = $parent['name'] ?? '';
			if($parent["pid"] > 0){
				$parent2 = self::where("id", $parent["pid"])->find();
				if(!empty($parent2)){
					$arr[] = $parent2['name'] ?? '';
					$parent3 = self::where("id", $parent2["pid"])->find();
					if(!empty($parent3)){
						$arr[] = $parent3['name'] ?? '';
					}
				}
			}
		}
		return !empty($arr) ? implode("/", array_reverse($arr)) : '';
	}



	/**
	 * 维修项目分类管理列表 - 编辑
	 * @param $param
	 * @return ConfigBanner
	 */
	public static function updateRepairCat($param = [])
	{
		$update["update_time"] = time();  //更新时间
		if(!empty($param["pid"]))       $update["pid"] = $param["pid"];
		if(!empty($param["name"]))      $update["name"] = $param["name"];
		if(!empty($param["img"]))       $update["img"] = $param["img"];
		if(!empty($param["sort"]))      $update["sort"] = $param["sort"];
		if(!empty($param["level"]))     $update["level"] = $param["level"];
		if(!empty($param["format_type"])) $update["format_type"] = $param["format_type"];
		if(!empty($param["recommend"])) $update["recommend"] = $param["recommend"];

		return self::where("id", $param['id'])->update($update);
	}



	/**
	 * 添加项目分类
	 * @param $param
	 * @return int|string
	 */
	public static function addRepairCat($param = [])
	{
		$add = [
			"pid"           => $param["pid"],
			"name"          => $param["name"] ?? '',  //细分分类
			"img"           => $param["img"] ?? '',  //客户端类型
			"sort"          => $param["sort"] ?? '',  //是否显示 1显示 2不显示
			"level"         => $param["level"] ?? '',
			"format_type"   => $param["format_type"] ?? '2',
			"recommend"     => $param["recommend"] ?? '2',
			"create_time"   => time(),
		];
		return self::insert($add);
	}




	/**
	 * 维修项目分类管理列表 - 启用禁用
	 * @param $param
	 * @return string
	 */
	public static function enableRepairCat($param = [])
	{
		//获取用户发送的id，根据状态修改当前banner的启用禁用状态
		$status = self::where("id", $param['id'])->value("status");
		$msg = '';
		if($status == 1){   //1启用 2禁用
			$enable = 2;
			$msg = "禁用成功";
		}else{
			$enable = 1;
			$msg = "启用成功";
		}
		self::where("id", $param['id'])->update(['status' => $enable]);
		return $msg;
	}



	public static function getRepairCatData($id = 0)
	{
		$result = [];
		$item = self::whereNull("delete_time")
			->where("status", 1)
			->where('pid',$id)->select()->toArray();
		if ($item){
			foreach($item as $k=> $v) {
				$item[$k]['children'] = [];
				$data = self::getRepairCatData($v['id']);
				if ($data){
					foreach($data as $kk=> $vs) {
						$item[$k]['children'][$kk] = $vs;
					}
				}
				$result[$k] =$item[$k];
			}
		}
		return $result;
	}


	public static function getApiRepairCatData($id = 0, $name = "")
	{
		$result = [];
		$item = self::whereNull("delete_time")
			->where("status", 1)
			->where('pid',$id)
			->where(function ($query) use ($name){
				if(!empty($name)) $query->where("name", $name);
			})
			->select()->toArray();
		if ($item){
			foreach($item as $k=> $v) {
				$item[$k]['children'] = [];
				$data = self::getApiRepairCatData($v['id'], "");
				if ($data){
					foreach($data as $kk=> $vs) {
						$item[$k]['children'][$kk] = $vs;
					}
				}
				$result[$k] =$item[$k];
			}
		}
		return $result;
	}


}